package com.liveHere.www.dao;

import com.liveHere.www.Exception.UserDaoException;
import com.liveHere.www.po.Room;
import com.liveHere.www.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author Mo312
 */
public class RoomDaoImpl implements RoomDao {

    //获取房间
    @Override
    public ArrayList<Room> getAllRooms(int shopId) throws Exception {
        String sql = "select * from rooms where shop_id=?";
        ArrayList<Room> rooms = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, shopId);
            rs = pstmt.executeQuery();
            Room room = new Room();
            while (rs.next()) {
                room.setRoomId(rs.getInt("room_id"));
                room.setShopId(rs.getInt("shop_id"));
                room.setRoomName(rs.getString("room_name"));
                room.setRoomType(rs.getString("room_type"));
                room.setKeywords(rs.getString("keywords"));
                room.setPrice(rs.getDouble("price"));
                room.setRemaining(rs.getInt("remaining"));
                rooms.add(room);

            }
            return rooms;
        } catch (SQLException e) {
            throw new Exception("获取房间列表失败: " + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, rs);
        }
    }

    //更新房间数量
    @Override
    public boolean updateRoomRemaining(int roomId, int remaining) throws SQLException {
        String sql = "UPDATE rooms SET remaining = ? WHERE room_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, remaining);
            pstmt.setInt(2, roomId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new SQLException("更新房间数量失败：" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, null);
        }
    }

    // 根据店铺ID查询房型信息
    @Override
    public ArrayList<Room> getRoomsByShopId(int shopId) throws SQLException {
        String sql = "SELECT * FROM rooms WHERE shop_id = ?";
        ArrayList<Room> rooms = new ArrayList<>();

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, shopId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Room room = new Room();
                room.setRoomId(rs.getInt("room_id"));
                room.setShopId(rs.getInt("shop_id"));
                room.setRoomName(rs.getString("room_name"));
                room.setRoomType(rs.getString("room_type"));
                room.setPrice(rs.getDouble("price"));
                room.setRemaining(rs.getInt("remaining"));
                room.setKeywords(rs.getString("keywords"));
                rooms.add(room);
            }
        } catch (Exception e) {
            throw new SQLException("查询房型信息失败：" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, rs);
        }
        return rooms;
    }

    // 根据房型ID查询房型信息
    @Override
    public Room getRoomById(int roomId) throws SQLException {
        String sql = "SELECT * FROM rooms WHERE room_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, roomId);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                Room room = new Room();
                room.setRoomId(rs.getInt("room_id"));
                room.setShopId(rs.getInt("shop_id"));
                room.setRoomName(rs.getString("room_name"));
                room.setRoomType(rs.getString("room_type"));
                room.setPrice(rs.getDouble("price"));
                room.setRemaining(rs.getInt("remaining"));
                room.setKeywords(rs.getString("keywords"));
                return room;
            }
        } catch (Exception e) {
            throw new SQLException("查询房型信息失败：" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, rs);
        }
        return null;
    }

    // 添加房型
    @Override
    public boolean addRoom(Room room) throws Exception {
        String sql = "INSERT INTO rooms (room_id,shop_id, room_name, room_type,keywords,price, remaining) VALUES (?,?,?,?,?,?,?)";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, room.getRoomId());
            pstmt.setInt(2, room.getShopId());
            pstmt.setString(3, room.getRoomName());
            pstmt.setString(4, room.getRoomType());
            pstmt.setString(5, room.getKeywords());
            pstmt.setDouble(6, room.getPrice());
            pstmt.setInt(7, room.getRemaining());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new Exception("添加房型失败", e);
        } finally {
            JDBCUtils.close(conn, pstmt, null);
        }
    }

    // 删除房型
    @Override
    public boolean deleteRoom(int roomId) throws Exception {
        String sql = "DELETE FROM rooms WHERE room_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, roomId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new Exception("删除房型失败" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, null);
        }
    }

    // 修改房型
    @Override
    public boolean updateRoom(Room room) throws Exception {
        String sql = "UPDATE rooms SET room_name = ?, room_type= ?,keywords=?, price=?,remaining = ? WHERE room_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, room.getRoomName());
            pstmt.setString(2, room.getRoomType());
            pstmt.setString(3, room.getKeywords());
            pstmt.setDouble(4, room.getPrice());
            pstmt.setInt(5, room.getRemaining());
            pstmt.setInt(6, room.getRoomId());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new Exception("修改房型失败" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, null);
        }
    }
}
